//
//  ViewController.m
//  ScoreManager
//
//  Created by 何凯楠 on 2017/4/19.
//  Copyright © 2017年 HeXiaoBa. All rights reserved.
//

#import "ViewController.h"
#import "XTSqliteUtils.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *uploadButton;
@property (nonatomic, nullable, strong) NSArray *students;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self getData];
}

- (void)getData {
    
    self.students = [XTSqliteUtils queryStudents];
    [self.tableView reloadData];
}

#pragma mark- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.students.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    if (self.students.count > indexPath.row) {
        NSDictionary *dic = self.students[indexPath.row];
        cell.textLabel.text = dic[@"username"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.f分", [dic[@"score"] doubleValue]];
    }
    
    return cell;
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger type = [[[NSUserDefaults standardUserDefaults] objectForKey:@"type"] integerValue];
    NSString *username = nil;
    if (self.students.count > indexPath.row) {
        NSDictionary *dic = self.students[indexPath.row];
        username = dic[@"username"];
    }
    if (type) {
        UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"编辑分数" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UITextField *scoreField =  alert.textFields[0];
                NSString *score = scoreField.text;
                if (score.length) {
                    BOOL flag = [XTSqliteUtils updateSocre:[score doubleValue] username:username];
                    if (flag) {
                        [self getData];
                    }
                }
            }];
            [alert addAction:cancel];
            [alert addAction:sure];
            [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                textField.placeholder = @"请输入分数";
            }];
            [self.navigationController presentViewController:alert animated:YES completion:nil];
        }];
        UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            BOOL flag = [XTSqliteUtils deleteUserWithUsename:username];
            if (flag) {
                [self getData];
            }
        }];
        return @[delete, edit];
    }
    return @[];
}


- (IBAction)logout:(id)sender {
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"username"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"type"];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    [UIApplication sharedApplication].keyWindow.rootViewController = [main instantiateViewControllerWithIdentifier:@"login"];
}

- (IBAction)upload:(id)sender {
    
}


@end
